package com.wn.naturalbiorhythms.tool;

import java.util.Date;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class BirthdaySettingsSharePref {

	private static final String NAME = "BirthdaySettings_spref";
	private static final String KEY_BIRTYDAY = "birtyday-";
	private static final String KEY_SEX = "sex-";
	private static final String KEY_MODEL = "model";
	private static final String KEY_GRAPHIC_SHOWINGDAYS = "graphic_showingdays";
	private static final String KEY_MOVE_TO = "move_to";
	private static final String KEY_BIRTHDAY_ISCHANGED = "birthday_ischanged";
	private static SharedPreferences spref ;
	private static BirthdaySettingsSharePref bsspref;
	private static Context context;
	private static Editor editor;
	
	public static BirthdaySettingsSharePref getInstance(Context c){
		if(bsspref == null){
			bsspref = new BirthdaySettingsSharePref(c);
		}
		
		return bsspref;
	}
	
	private BirthdaySettingsSharePref(Context c){
		context =c;
		spref = context.getSharedPreferences(NAME, Context.MODE_PRIVATE);
		editor = spref.edit();
	}
	
	public void saveBirthday(byte userId,long birthday){
		editor.putLong(KEY_BIRTYDAY+userId, birthday).commit();
		saveBirthdayIsChanged(userId, true);
	}
	
	public void saveSex(byte userId,byte sex){
		editor.putInt(KEY_SEX+userId, sex).commit();
	}
	
	public long getBirthday(byte userId){
		saveBirthdayIsChanged(userId, false);
		return spref.getLong(KEY_BIRTYDAY+userId, new Date().getTime());
	}
	
	public byte getSex(byte userId){
		return (byte)spref.getInt(KEY_SEX+userId, 0);
	}
	
	public void saveModel(byte model){
		editor.putInt(KEY_MODEL, model).commit();
	}
	
	public byte getModel(){
		return (byte)spref.getInt(KEY_MODEL, 0);
	}
	
	public void saveShowingDays(byte daysId){
		editor.putInt(KEY_GRAPHIC_SHOWINGDAYS, daysId).commit();
	} 
	
	public byte getShowingDaysId(){
		return (byte)spref.getInt(KEY_GRAPHIC_SHOWINGDAYS, 0);
	}
	
	public void saveMoveTo(byte dayId){
		editor.putInt(KEY_MOVE_TO, dayId).commit();
	} 
	
	public byte getMoveToDayId(){
		return (byte)spref.getInt(KEY_MOVE_TO, 0);
	}
	
	public void saveBirthdayIsChanged(byte userId,boolean isChanged){
		editor.putBoolean(KEY_BIRTHDAY_ISCHANGED + userId, isChanged).commit();
	}
	
	public boolean birthdayIsChanged(byte userId){
		return spref.getBoolean(KEY_BIRTHDAY_ISCHANGED + userId, false);
	} 
}
